#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
/**
 * 
 */
package ${package}.bean;

import java.util.ArrayList;
import java.util.HashSet;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.DualListModel;

import ${package}.dao.api.UsuarioDao;
import ${package}.model.Papel;
import ${package}.model.Usuario;
import ${package}.negocio.api.PapelNegocio;
import ${package}.negocio.api.UsuarioNegocio;
import br.com.framework.apresentacao.bean.CRUDBean;
import br.com.framework.persistencia.sort.Order;

/**
 * @author Cleber
 *
 */
@Named
@ConversationScoped
public class UsuarioBean extends CRUDBean<Usuario, UsuarioDao, UsuarioNegocio>{
	
	@Inject
	private PapelNegocio papelNegocio;
	
	private DualListModel<Papel> papeis;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * @param entidadeClass
	 */
	public UsuarioBean() {
		super(Usuario.class);
	}
	
	public DualListModel<Papel> getPapeis() {
		return papeis;
	}
	
	public void setPapeis(DualListModel<Papel> papeis) {
		this.papeis = papeis;
	}
	
	@Inject
	protected void setNegocio(UsuarioNegocio negocio) {
		super.setNegocio(negocio);
	}
	
	@Override
	protected void beforeOutcomeCreate() {
		papeis = new DualListModel<Papel>();
		papeis.setSource(papelNegocio.findAll("nome", Order.ASCENDING, true));
		papeis.setTarget(new ArrayList<Papel>());
	}
	
	@Override
	protected void beforeOutcomeEdit() {
		papeis = new DualListModel<Papel>();
		papeis.setSource(papelNegocio.findAll("nome", Order.ASCENDING, true));
		papeis.setTarget(new ArrayList<Papel>(getEntidade().getPapeis()));
		papeis.getSource().removeAll(getEntidade().getPapeis());
		super.beforeOutcomeEdit();
	}
	
	@Override
	public String salvar() {
		if (getEntidade().getPapeis() == null) {
			getEntidade().setPapeis(new HashSet<Papel>());
		}
		getEntidade().getPapeis().addAll(getPapeis().getTarget());
		getEntidade().getPapeis().retainAll(getPapeis().getTarget());
		return super.salvar();
	}

}
